const Mock = require('mockjs')
const Random = Mock.Random
const List = []

Random.extend({
  constellation: function(date) {
    var constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']
    return this.pick(constellations)
  },
  gender_ch: function(boolean) {
    var genders = ['男', '女']
    return this.pick(genders)
  },
  major: function() {
    var majors = ['计算机科学与技术', '软件工程', '临床医学', '生化环材']
    return this.pick(majors)
  },
  org: function() {
    var majors = ['计算机服务队', '软件俱乐部', '自行车协会', '红会', '学生会']
    return this.pick(majors)
  },
  orgs: function() {
    const org = ['计算机服务队', '软件俱乐部', '自行车协会', '红会', '学生会']
    const orgs = []
    var i = Random.integer(1, 5)
    while (i-- > 0) {
      orgs.push(this.pick(org))
    }
    return unique(orgs)
  },
  participants: function() {
    const mans = []
    var i = Random.integer(1, 10)
    while (i-- > 0) {
      mans.push(Random.first())
    }
    return mans
  },
  status: function() {
    var statuss = ['申请中', '接受', '拒绝', '已过期']
    return this.pick(statuss)
  },
  place: function() {
    const map = ['rear', 'prev', 'none']
    const placeAll = {
      'rear': ['教学楼A-', '教学楼B-', '教学楼C-'],
      'prev': ['号篮球场', '号排球场'],
      'none': ['体育馆', '图书馆会议室', '咖啡厅']
    }
    const type = this.pick(map)
    if (type === 'rear') {
      return this.pick(placeAll[type]) + Random.integer(1, 300)
    } else if (type === 'prev') {
      return Random.integer(1, 20) + this.pick(placeAll[type])
    } else {
      return this.pick(placeAll[type])
    }
  },
  open: function() {
    const scale = ['只向名单内人员开放', '校内开放', '公众开放']
    return this.pick(scale)
  }
})

for (let i = 0; i < 100; i++) {
  List.push(Mock.mock({
    id: i + 1,
    activityName: '@string(10)',
    userId: i + 1,
    hostname: '@first',
    hostOrg: '@Org',
    needPlace: '@Place',
    participantList: '@Participants',
    participantOrgList: '@Orgs',
    scale: '@string(number,3)' + '人',
    status: '@Status',
    intro: '@string(40)',
    imgList: [
      'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fbd315c6034a85edf1ef4cc334a540923dd5475a1.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620506668&t=43008dd2f5e6b58ec562397499ed5c71',
      'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fbd315c6034a85edf1ef4cc334a540923dd5475a1.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620506668&t=43008dd2f5e6b58ec562397499ed5c71',
      'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fbd315c6034a85edf1ef4cc334a540923dd5475a1.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620506668&t=43008dd2f5e6b58ec562397499ed5c71'
    ],
    open: '@Open'
  }))
}

module.exports = [
  {
    url: '/org-manage-system/activity/list',
    type: 'get',
    response: config => {
      const { type, title, page, limit, status } = config.query
      console.log(title)
      const mockList = List
      console.log(status)
      const listAfterFilter = mockList.filter(
        (item, index) => {
          var flag = true
          let statusFlag = true
          if (title != null && title !== '') {
            flag = false
            if (type != null && type !== '') {
              flag = item[type].search(title) >= 0
            } else {
              flag = item.name.search(title) >= 0 || item.org.search(title) >= 0
            }
          }
          if (status === '0') {
            statusFlag = item.status.search('拒绝') >= 0
          } else if (status === '1') {
            statusFlag = item.status.search('申请中') >= 0
          } else if (status === '2') {
            statusFlag = item.status.search('接受') >= 0
          } else {
            statusFlag = true
          }
          return flag && statusFlag
        }
      )
      const pageList = listAfterFilter.filter(
        (item, index) => {
          return index < limit * page && index >= limit * (page - 1)
        })
      return {
        status: 200,
        data: {
          total: listAfterFilter.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/org-manage-system/activity/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      var mockList = List
      return {
        status: 200,
        data: {
          items: mockList[id - 1]
        }
      }
    }
  }
]
function unique(arr) {
  var hash = []
  for (var i = 0; i < arr.length; i++) {
    for (var j = i + 1; j < arr.length; j++) {
      if (arr[i] === arr[j]) {
        ++i
      }
    }
    hash.push(arr[i])
  }
  return hash
}
